﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTOs;
using Entities.Database;

namespace Entities
{
    public class ProductManager
    {

        public void AddProductCategory(string name, string code)
        {
            using (var entities = new testMoveDBEntities())
            {

                entities.ProductCategories.AddObject(new ProductCategory()
                    {
                        Name = name,
                         Code = code,
                        IsVisible = true
                    });

                entities.SaveChanges();

            }
        }

        public List<ProductCategoryDTO> GetProductCategories()
        {
            List<ProductCategoryDTO> categs = new List<ProductCategoryDTO>();
            using (var entities = new testMoveDBEntities())
            {
                categs = entities.ProductCategories.Where(c => c.IsVisible == true).Select(p => new ProductCategoryDTO()
                 {
                     Id = p.CategoryId,
                     Name = p.Name
                 }).ToList();
            }

            //categs.Add(new ProductCategoryDTO() { Id = 1, Name = "cake" });
            //categs.Add(new ProductCategoryDTO() { Id = 2, Name = "food" });
            //categs.Add(new ProductCategoryDTO() { Id = 3, Name = "cloth" });

            return categs;

        }


        public void AddProduct(ProductDTO productDetail)
        {
            using (var entities = new testMoveDBEntities())
            {
                string code = "FRN";
                
                try
                {
                  string lastCode =  entities.Products.Where(p => p.CategoryId == productDetail.CategoryId).OrderByDescending(c => c.ProductId).First().ProductCode;

                  string strCode = lastCode.Substring(0,3);
                  int intCode = int.Parse( lastCode.Remove(0, 3));

                  code = strCode + (intCode + 1).ToString().PadLeft(3, '0');
                }
                catch
                {

                    try
                    {
                        code = entities.ProductCategories.First(p => p.CategoryId == productDetail.CategoryId).Code;
                        code = code + "00" + 1;
                    }
                    catch
                    {

                    }
                }
                entities.Products.AddObject(new Product()
                {
                    CategoryId = productDetail.CategoryId,
                    CreatedOn = DateTime.Now,
                    Description = productDetail.Description,
                    Detail = productDetail.Detail,
                    Image = productDetail.Image,
                    Name = productDetail.Name,
                    Price = productDetail.Price,
                    ProductCode = code
                });

                entities.SaveChanges();
            }

        }


        public PagedData<ProductDTO> GetProductDetails(int type, int pageNumber, int pageSize)
        {
            var products = new PagedData<ProductDTO>();

            using (var ctx = new testMoveDBEntities())
            {
                //people.Data = ctx.People.OrderBy(p => p.Surname).Skip(PageSize * (page - 1)).Take(PageSize).ToList();
                //people.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)ctx.People.Count() / PageSize));
                //people.CurrentPage = page;

                products.Data = ctx.Products.OrderBy(p => p.Name).Skip(pageSize * (pageNumber -1)).Take(pageSize).Select(p => new ProductDTO()
                {
                    Name = p.Name,
                    ProductCode = p.ProductCode,
                    ProductId = p.ProductId,
                    Image = p.Image,
                    Price = p.Price,
                    Description= p.Description
                }).ToList();
                products.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)ctx.Products.Count() / pageSize));

                //List<ProductDTO> prodcts = new List<ProductDTO>();
                //prodcts.Add(new ProductDTO(){ CategoryId=2, Description  ="this is ice cake" , Detail ="This is detail", Image="icecake.jpg", Name="ice cake", Price= 120, ProductCode="CAK001", ProductId = 1});
                //prodcts.Add(new ProductDTO(){ CategoryId=2, Description  ="this is ice cake2" , Detail ="This is detail2", Image="chococake.jpg", Name="ice cake2", Price= 122, ProductCode="CAK002", ProductId = 2});
                //prodcts.Add(new ProductDTO(){ CategoryId=2, Description  ="this is ice cake3" , Detail ="This is detail3", Image="icecake.jpg", Name="ice cake3", Price= 123, ProductCode="CAK003", ProductId = 3});
                //prodcts.Add(new ProductDTO(){ CategoryId=2, Description  ="this is ice cake4" , Detail ="This is detail4", Image="chococake.jpg", Name="ice cake4", Price= 124, ProductCode="CAK004", ProductId =4});


                //products.Data = prodcts;
                //products.NumberOfPages = 2;
                ////products.Data
                products.CurrentPage = pageNumber;

            }

            return products;
        }        
   
           
    
    }
}
